﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Collections.Generic;
using chessLogic;

namespace chessView
{
    public class Drawer
    {
        private int squarySide = 40;

        public void DrawBoard(Graphics graphic)
        {
            Rectangle square = new Rectangle(0, 0, squarySide, squarySide);
            SolidBrush brush = new SolidBrush(Color.White);
            for (int i = 0; i < 8; i++)
            {
                square.X = 0;
                for (int j = 0; j < 8; j++)
                {
                    if ((j + i) % 2 == 0)
                        brush.Color = Color.Gray;
                    else
                        brush.Color = Color.DarkGray;
                    graphic.FillRectangle(brush, square);
                    square.X += squarySide;
                }
                square.Y += squarySide;
            }
        }
        public void DrawFiguresOnBoard(Graphics graphic, Board board)
        {
            for (int i = 0; i < 8; i++)
            {
                foreach (char j in "abcdefgh")
                {
                    Position position = new Position(j, i);
                    if (board.getFigure(position).Type != FigureType.None)
                        DrawFigure(board.getFigure(position), position, graphic);
                }
            }
        }
        public void DrawFigure(Figure figure, Position pos, Graphics graphic)
        {
            Bitmap figureBmp = Properties.Resources.ResourceManager.GetObject(GetResourceObject(figure)) as Bitmap;
            figureBmp.MakeTransparent(figureBmp.GetPixel(1, 1));
            string letters = "abcdefgh";
            int indexChar = letters.IndexOf(pos.Letter);
            graphic.DrawImage(figureBmp, indexChar * squarySide, -1 * (pos.Number - 7) * squarySide, squarySide, squarySide);
        }
        public void DrawMoveSquare(Graphics graphic, Position pos)
        {
            int indexChar = pos.GetCollNum();
            Rectangle square = new Rectangle(3 + indexChar * squarySide, 3 - 1 * (pos.Number - 7) * squarySide, squarySide - 5, squarySide - 5);
            //graphic.DrawRectangle(new Pen(Color.DarkBlue, 2), square);
            graphic.FillRectangle(new SolidBrush(Color.DarkSlateGray), square);
        }
        private string GetResourceObject(Figure figure)
        {
            string figureBmp = "b";
            if (figure.Color == FigureColor.White)
                figureBmp = "w";
            switch (figure.Type)
            {
                case FigureType.Pawn:
                    figureBmp += "pawn";
                    break;
                case FigureType.Rook:
                    figureBmp += "rook";
                    break;
                case FigureType.King:
                    figureBmp += "king";
                    break;
                case FigureType.Knight:
                    figureBmp += "knight";
                    break;
                case FigureType.Bishop:
                    figureBmp += "bishop";
                    break;
                case FigureType.Queen:
                    figureBmp += "queen";
                    break;
            }
            return figureBmp;
        }
        public void DrawPointer(Graphics graphic, Position posStart, Position posEnd, int displace)
        {
            graphic.SmoothingMode = SmoothingMode.AntiAlias;
            string letters = "abcdefgh";
            int displaceX = 5;
            int displaceY = 5;
            Pen pen;
            if (displace % 2 == 0)
                pen = new Pen(Color.Blue, 2);
            else
                pen = new Pen(Color.Red, 2);
            if (displace % 4 == 1)
            {
                displaceX = -5;
                displaceY = 5;
            }
            else if (displace % 4 == 2)
            {
                displaceX = -5;
                displaceY = -5;
            }
            else if (displace % 4 == 3)
            {
                displaceX = 5;
                displaceY = -5;
            }
            pen.StartCap = LineCap.Round;
            pen.EndCap = LineCap.ArrowAnchor;
            graphic.DrawLine(pen, (letters.IndexOf(posStart.Letter) * squarySide) + 20 + displaceX, (-1 * (posStart.Number - 7) * squarySide) + 20 + displaceY, (letters.IndexOf(posEnd.Letter) * squarySide) + 20 + displaceX, (-1 * (posEnd.Number - 7) * squarySide) + 20 + displaceY);
            pen.Dispose();
        }
    }
}